import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

class ComplexFixed extends StatelessWidget {
  const ComplexFixed({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,
      home: ScrollPage(),
    );
  }
}


class ScrollPage extends StatefulWidget {
  const ScrollPage({super.key});

  @override
  State<ScrollPage> createState() => _ScrollPageState();
}

class _ScrollPageState extends State<ScrollPage> with TickerProviderStateMixin {
  ScrollController _scrollController = ScrollController();
  late AnimationController _controller;
  late Animation<double> _animation;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    super.initState();
    _controller = AnimationController(vsync: this, duration: const Duration(milliseconds: 300));

    _animation = Tween<double>(begin: 1.0, end: 0.0).animate(_controller)
    ..addListener(() {
        setState(() {
          
        });
      });

    // _controller.forward();

    _scrollController.addListener(_scroll);
  }

  _scroll() {
    if (_scrollController.position.userScrollDirection == ScrollDirection.forward) {
      print(111111);
      print(_scrollController.offset);
      if (_scrollController.offset <= 0) {
        _controller.reverse();
      }
    } else {
      _controller.forward();
    }
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _controller.dispose();
    super.dispose();
    _scrollController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          if (_controller.isCompleted) {
            _controller.reverse();
          } else {
            _controller.forward();
          }
        }
      ),
      appBar: AppBar(
        toolbarHeight: 100 - 50 * (1 - _animation.value),
        titleSpacing: 0.0,
        title: Container(
          width: double.infinity,
          height: 100 - 50 * (1 - _animation.value),
          color: Colors.grey,
          child: Stack(
            children: [
              const Row(
                children: [
                  Text('logo'),
                  Expanded(child: SizedBox()),
                  Icon(Icons.notifications)
                ],
              ),
              Positioned(
                bottom: 5,
                left: 50 * (1 - _animation.value),
                child: Container(
                  width: 360 - 100 * (1 - _animation.value),
                  height: 40,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(20),
                    color: Colors.blue[200],
                  ),
                )
              )
            ],
          ),
        ),
      ),
      body: ListView.builder(
        controller: _scrollController,
        itemCount: 50,
        itemBuilder: (BuildContext context, int index) {
          return ListTile(
            title: Text('第 $index 行'),
            subtitle: Text('副标题：第 $index 行'),
          );
        }
      ),
    );
  }
}